﻿using HBase;
using System.Net.Sockets;
using System.Net;
using System.Management;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Diagnostics;

namespace http {
    internal class Program {
        public static List<String> ips;
      
        static void Main(string[] args) {

            Console.WriteLine("Hello, World!");
            ips = Utils.readFile();
            HATask t= new HATask();
            t.vip = ips[0];
           // t.start(); 
            while (true) {
                myChecked(t);
                Thread.Sleep(1000);
            }
        }


        static String getRunIp() {
            for (int i = 1; i < ips.Count; i++) {
                String relIp = ips[i];

                String url = "http://" + relIp + ":12138/open/running";
                Console.WriteLine("textIp" + url);
                DateTime n = DateTime.Now;
                String res = HttpRequestUrl.httpGet(url, false, 500);
                 Console.WriteLine((DateTime.Now.Ticks-n.Ticks)/10000+"ms"+url);
                if (res.Length > 10) {
                    return relIp;
                }
            } 
            return null;

        }


        static void myChecked(HATask t) {

            String relIp = getRunIp();
            Console.WriteLine("runIp" + relIp);
            
            if (relIp == null) {
                return;
            }
            ManagementObject mo = getMo(relIp); 
            if (mo != null) {//本机器启动
                Console.WriteLine("myrun");
                setIPaddress(relIp, ips[0]);//用管理员启动
                Console.WriteLine("add ip");
                HttpRequestUrl.httpGet("http://127.0.0.1:12138/open/setrunning", false, 500);
                Console.WriteLine("server run ip");
                t.meWork = true;
             
            } else {
                Console.WriteLine("mystop");
                t.meWork = false;
                deleteIPaddress(ips[0]);
                Console.WriteLine("delete ip");
                HttpRequestUrl.httpGet("http://127.0.0.1:12138/open/stop", false, 500);
                Console.WriteLine("server stop ip");

            } 
               
            
    
        


        }




        static  ManagementObject getMo(String mip) {
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc) {
                if (!(bool)mo["IPEnabled"]) {
                    continue;
                }

                System.Array ar = (System.Array)(mo.Properties["IPAddress"].Value);
                foreach (object o in ar) {

                    if (o.ToString() == mip) {
                        return mo;
                    }


                }
            }
            return null;
        }
       
        static String getName(Process p) {
            p.StandardInput.WriteLine("ipconfig -all");
            String name = "";
            while (true) {
                String line = p.StandardOutput.ReadLine();
                if (hasIp(line)) { 
                    break;
                }
                int index = line.IndexOf("适配器 ");
                if (index > 0) {
                    name = line.Substring(index + 4);
                    name = name.Substring(0, name.Length - 1);
                }


            }
            return name;
        }

        static bool hasIp(String l) {
            for (int i = 1; i < ips.Count; i++) {
                if (l.IndexOf(ips[i]) != -1) {
                    return true;
                }
            }
            return false;
        }


        static void setIPaddress(String mip,String vip ) {

            changeIP("netsh interface ipv4 add address  {0}  "+ vip + " 255.255.255.0 "); 
        }
        static void deleteIPaddress(String vip) {
            changeIP("netsh interface ipv4 delete address  {0}  "+ vip + "  ");
        }

        static void changeIP(String cmd ) {
          


            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();

            String name = getName(p);
            string d = string.Format(cmd, name);
            p.StandardInput.WriteLine(d);
            p.StandardInput.WriteLine("exit");

        }



    }
}